//
//  ViewController.m
//  TSS-Signing
//
//  Created by cole cabral on 2018-06-13.
//  Copyright © 2018 cole cabral. All rights reserved.
//

#import "ViewController.h"
#import "devices.h"
#import "LCLoadingHUD.h"

#pragma mark - BaseClass -

@implementation BaseClass

@end

#pragma mark - firmwares -

@implementation Firmwares

@end
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *firmwareArray;
    NSMutableArray *nameArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)fetchFirmware {
    //调用方法
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //调用结束
    [LCLoadingHUD showLoading:(@"Please wait...", @"Please wait...") inView:self.view];
    [NSTimer scheduledTimerWithTimeInterval:3.0f/*设置加载显示时长*/ target:self selector:@selector(dismissInViewHUD) userInfo:nil repeats:NO];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *error;
        //加载一个NSURL对象  iPhone10,3
       // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/%@?type=ipsw", deviceModel]]];
        NSURLRequest *BaseClass = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/%@?type=ipsw",deviceModel]]];
        //将请求的url数据放到NSData对象中
        NSData *response = [NSURLConnection sendSynchronousRequest:BaseClass returningResponse:nil error:nil];
        if (response) {
            NSDictionary *BaseClass = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
            NSDictionary *firmwares = [BaseClass objectForKey:@"firmwares"];
            self->firmwareArray = [NSMutableArray array];
            self->nameArray = [NSMutableArray array];
            for (id firm in firmwares) {
                BOOL signedStatus = [[firm objectForKey:@"signed"] boolValue];
                
                    [self->firmwareArray addObject:firm];
                    [self.tableView reloadData];
                
            }
            NSLog(@"weatherInfo字典里面的内容为--》%@", firmwares );
        }
    });
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self fetchFirmware];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)backgroundUpdate:(void (^)(UIBackgroundFetchResult))completionHandler {
    [self fetchFirmware];
    completionHandler(UIBackgroundFetchResultNewData);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

- (void)dismissKeyHUD {
    
    [LCLoadingHUD hideInKeyWindow];
}

- (void)dismissInViewHUD {
    
    [LCLoadingHUD hideInView:self.view];
}

#pragma mark -

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [firmwareArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"tss";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    NSDictionary *firmware = [firmwareArray objectAtIndex:indexPath.row];
    BOOL signedStatus = [[firmware objectForKey:@"signed"] boolValue];
    if (signedStatus != 0){
        cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"型号:%@   ✅固件正在签名，你可使用iTunes来恢复你的Apple设备", [firmware objectForKey:@"identifier"]];
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"], [firmware objectForKey:@"identifier"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"型号:%@  ❌固件未签名", [firmware objectForKey:@"identifier"]];
    }
    /*if (signedStatus != 0){
        cell.textLabel.text = [NSString stringWithFormat:@"Version:%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"正在签名，可刷机✅"];
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"Version:%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"未签名,不可刷机❌"];
    }*/
    return cell;
}
- (IBAction)RE:(id)sender {
    //调用方法
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //调用结束
    [LCLoadingHUD showLoading:(@"Please wait...", @"Please wait...") inView:self.view];
    [NSTimer scheduledTimerWithTimeInterval:3.0f/*设置加载显示时长*/ target:self selector:@selector(dismissInViewHUD) userInfo:nil repeats:NO];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *error;
        //加载一个NSURL对象  iPhone10,3
        // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/%@?type=ipsw", deviceModel]]];
        NSURLRequest *BaseClass = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/iPhone10,3?type=ipsw"]]];
        //将请求的url数据放到NSData对象中
        NSData *response = [NSURLConnection sendSynchronousRequest:BaseClass returningResponse:nil error:nil];
        if (response) {
            NSDictionary *BaseClass = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
            NSDictionary *firmwares = [BaseClass objectForKey:@"firmwares"];
            self->firmwareArray = [NSMutableArray array];
            self->nameArray = [NSMutableArray array];
            for (id firm in firmwares) {
                BOOL signedStatus = [[firm objectForKey:@"signed"] boolValue];
                
                [self->firmwareArray addObject:firm];
                [self.tableView reloadData];
                
            }
            NSLog(@"weatherInfo字典里面的内容为--》%@", firmwares );
        }
    });
}
@end
